package com.example.lyqwork.service;


import com.example.lyqwork.pojo.User;
import com.example.lyqwork.pojo.dto.UserDto;
import com.example.lyqwork.pojo.dto.UserEditDto;
import com.example.lyqwork.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service //配置成bean
public class UserService implements IUserService {

    @Autowired
    UserRepository userRepository;

    @Override
    public User add(UserDto user) {
        //
        System.out.println(user.toString());
        User tempUser = new User();

        tempUser.setLogin_name(user.getLoginName());
        // 确保 UserDto 和 User 类的属性一致
        System.out.println(tempUser.toString());
        return userRepository.save(tempUser);
    }

    @Override
    public User getUser(Integer userId) {
        return userRepository.findById(userId).orElseThrow(() -> new RuntimeException("用户不存在,参数异常"));

    }

    @Override
    public User update(UserEditDto user) {
        User tempUser = new User();
        tempUser.setId(user.getUserId());
        tempUser.setLogin_name(user.getLoginName());
        // 确保 UserDto 和 User 类的属性一致
        return userRepository.save(tempUser);
    }

    @Override
    public void deleted (Integer userId) {
        userRepository.deleteById(userId);
    }
}

